package spr

import (
	pl "db2s/Or/spr/parser"
	"github.com/antlr4-go/antlr/v4"
)

func sqlDmlParse(node antlr.RuleNode) {
	switch node.(type) {
	case *pl.Select_statementContext:
		selectParse(node)
	case *pl.Update_statementContext:
		updateParse(node)
	case *pl.Delete_statementContext:
		deleteParse(node)
	case *pl.Insert_statementContext:
		insertParse(node)
	default:

	}
}
func sqlType(node antlr.RuleNode) {
	for _, child := range node.GetChildren() {
		switch childNode := child.(type) {
		case *pl.Select_statementContext, *pl.Update_statementContext, *pl.Delete_statementContext, *pl.Insert_statementContext: //dml
			sqlDmlParse(childNode.(antlr.RuleNode))
			return
		case antlr.TerminalNode:
		default:
			sqlType(childNode.(antlr.RuleNode))
		}
	}
}
func Parse(exPre string) {
	// 创建输入流
	input := antlr.NewInputStream(exPre)
	// 创建词法分析器
	lex := pl.NewPlSqlLexer(input)
	// 创建一个词法分析器的输出流
	stream := antlr.NewCommonTokenStream(lex, antlr.TokenDefaultChannel)
	// 创建一个语法分析器
	pars := pl.NewPlSqlParser(stream)
	queryCtx := pars.Sql_script()
	// 调用 Query 方法解析
	if queryCtx == nil {
		//log.Fatal("Query context is nil")
	}
	//var pp = &SelectParseS{}
	//plsql := &PlVisitor{
	//	Select: pp,
	//}
	sqlType(queryCtx)
	//queryCtx.Accept(plsql)
	//var ll []string
	//for _, v := range plsql.TableQueryBody.SelectColumn {
	//	fmt.Println("------sql column:", fmt.Sprintf("%v as %v %v", v.Value, v.AS, AnyToJsonString(v.CaseWhen)))
	//	ll = append(ll, v.Value)
	//}
	//fmt.Println("--jjj:", plsql.TableQueryBody.FromTable)
}
